<template>
  <el-dialog
    v-model="dialogVisible"
    title="新增模具"
    width="900px"
    @close="closeDialog"
  >
    <el-form class="aceauForm" label-width="120px" :model="state.form">
      <el-row :gutter="12">
        <el-col :span="12">
          <el-form-item label="模具编码" required>
            <el-input
              v-model="state.form.moldCode"
              placeholder="请填写编码"
              @click="toSelectInv"
              @focus="toSelectInv"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具名称">
            <el-input v-model="state.form.moldName" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具规格">
            <el-input v-model="state.form.moldSpec" placeholder="" />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="模具单位" required>
            <el-select
              v-model="state.form.pkMeasdoc"
              placeholder="计量"
              style="width: 100%"
            >
              <el-option
                v-for="measdoc in measdocs"
                :key="measdoc.pkMeasdoc"
                :label="measdoc.measName"
                :value="measdoc.pkMeasdoc"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="状态" required>
            <el-select
              v-model="state.form.state"
              placeholder="模具状态"
              style="width: 100%"
            >
              <el-option
                v-for="mlodState in mlodStates"
                :key="mlodState.value"
                :label="mlodState.label"
                :value="mlodState.value"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="备注">
            <el-input v-model="state.form.remark" placeholder="备注" />
          </el-form-item>
        </el-col>
      </el-row>
      <el-form-item label="" />
    </el-form>

    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="save">保存</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
  import { getAllMeasdocApi } from '@/api/base/measdoc/index'
  import { localStorageConfig } from '@/config/localstorage.config'
  import { addMoldDocApi } from '@/api/equip/mold_api'

  const $baseLoading = inject('$baseLoading')
  const $baseMessage = inject('$baseMessage')

  const pkCorp = localStorage.getItem(localStorageConfig.__pkCorp)
  const dialogVisible = ref(false)

  const state = reactive({
    form: {
      pkMeasdoc: '1001U81000000000E0U6',
      state: 1,
      pkCorp: pkCorp,
    },
  })

  const measdocs = ref([])

  const mlodStates = ref([
    { value: 1, label: '正常' },
    { value: 2, label: '停用' },
  ])

  const openDialog = () => {
    dialogVisible.value = true
    getMeasdoc()
  }

  const getMeasdoc = async () => {
    let conditions = {
      pkCorp,
    }
    const { code, data } = await getAllMeasdocApi(conditions)
    if (code == 2000) {
      measdocs.value = data
    }
  }

  const save = async () => {
    const load = $baseLoading()
    try {
      const res = await addMoldDocApi(state.form)
      if (res.code === 2000) {
        $baseMessage(res.msg, 'success')
        dialogVisible.value = false
        _emit('success-callback')
      } else {
        $baseMessage(res.msg, 'error')
      }
    } finally {
      load.close()
    }
  }
  const closeDialog = () => {
    dialogVisible.value = false
  }

  defineExpose({ openDialog })
  const _emit = defineEmits(['success-callback'])
</script>
